﻿#import os
#import sys




# Настройка вывода ошибок в файл

# Определяется текущая директория

#__dir__ = os.path.dirname(os.path.abspath(__file__))
#path_error = os.path.join(__dir__, "so_test_error.log")

# Вывод ошибок перенаправляется в файл error.log в текущей директории

#sys.stderr = open(path_error, 'a')



class postgre:
	"This class connects and disconnects postgre db"
	#print (db_postgre.__doc__)
	#beginning of class suite
	import postgresql
	import postgresql.driver as pg_driver
	user="palo"
	passwd="palo"
	host="sa-serv8"
	port=5432
	charset=None
	db="test_so"
	state=False
	log=None
	conn=None
	cursor=None

	def connect():
		"This function opens connection to postgre db"
		#print (postgre.connect.__doc__)
		#beginning of function suite
		if postgre.state==False:
			postgre.conn = postgre.pg_driver.connect(user = postgre.user, password = postgre.passwd, host = postgre.host, port = postgre.port, database = postgre.db)
			postgre.state=True
			postgre.log='Connected'
		else:
			postgre.log='Already connected'
		#end of function suite
		return
	
	def disconnect():
		"This function closes connection to postgre db"
		#print (postgre.disconnect.__doc__)
		#end of function suite
		if postgre.state==True:
			postgre.conn.close()
			postgre.conn=None
			postgre.state=False
			postgre.log='Disconnected'
		else:
			postgre.log='Already disconnected'
		#end of function suite
		return

	#end of class suite

class mysql:
	"This class connects and disconnects MySQL db"
	#print (mysql.__doc__)
	#beginning of class suite
	import cymysql
	user='palo'
	passwd='palo'
	host='sa-serv10.tb.local'
	port=None
	charset='utf8'
	db='palo'
	state=False
	log=None
	conn=None
	cursor=None

	def connect():
		"This function open connection to MySQL db"
		#print (mysql.connect.__doc__)
		#beginning of function suite
		if mysql.state==False:
			mysql.conn = mysql.cymysql.connect(host=mysql.host, user=mysql.user, passwd=mysql.passwd, db=mysql.db, charset=mysql.charset)
			mysql.cursor=mysql.conn.cursor()
			mysql.state=True
			mysql.log='Connected'
		else:
			mysql.log='Already connected'
		#end of function suite
		return 
	
	def disconnect():
		"This function closes connection to MySQL db"
		#print (mysql.disconnect.__doc__)
		#end of function suite
		if mysql.state==True:
			mysql.conn.close()
			mysql.conn=None
			mysql.cursor=None
			mysql.state=False
			mysql.log='Disconnected'
		else:
			mysql.log='Already disconnected'
		#end of function suite
		return

	#end of class suite

class firebird:
	"This class connects and disconnects Firebird db"
	#print (firebird.__doc__)
	#beginning of class suite
	import firebirdsql
	user='sysdba'
	passwd='imech'
	host='sa-fs7.tb.local'
	port=None
	charset=None
	db='C:\IM_SW\Search\DB\SEARCH4.GDB'
	state=False
	log=None
	conn=None
	cursor=None
	
	def connect():
		"This function open connection to Firebird db"
		#print (firebird.connect.__doc__)
		#beginning of function suite
		if firebird.state==False:
			firebird.conn = firebird.firebirdsql.connect(host=firebird.host, database=firebird.db, user=firebird.user, password=firebird.passwd)
			firebird.cursor = firebird.conn.cursor()
			firebird.state=True
			firebird.log='Connected'
		else:
			firebird.log='Already connected'
		#end of function suite
		return
	
	def disconnect():
		"This function closes connection to Firebird db"
		#print (firebird.disconnect.__doc__)
		#end of function suite
		if firebird.state==True:
			firebird.conn.close()
			firebird.conn=None
			firebird.cursor=None
			firebird.state=False
			firebird.log='Disconnected'
		else:
			firebird.log='Already disconnected'
		#end of function suite
		return

class firebird_fdb:
	import fdb
	"This class connects and disconnects Firebird db"
	#print (firebird_fdb.__doc__)
	#beginning of class suite
	user='sysdba'
	passwd='imech'
	host='sa-fs7.tb.local'
	port=None
	charset=None
	db='C:\IM_SW\Search\DB\SEARCH4.GDB'
	state=False
	log=None
	conn=None
	cursor=None
	
	def connect():
		"This function open connection to Firebird db"
		#print (firebird_fdb.connect.__doc__)
		#beginning of function suite
		if firebird_fdb.state==False:
			firebird_fdb.conn = firebird_fdb.fdb.connect(host=firebird_fdb.host, database=firebird_fdb.db, user=firebird_fdb.user, password=firebird_fdb.passwd)
			firebird_fdb.cursor = firebird_fdb.conn.cursor()
			firebird_fdb.state=True
			firebird_fdb.log='Connected'
		else:
			firebird_fdb.log='Already connected'
		#end of function suite
		return

	def disconnect():
		"This function closes connection to Firebird db"
		#print (firebird_fdb.disconnect.__doc__)
		#end of function suite
		if firebird_fdb.state==True:
			firebird_fdb.conn.close()
			firebird_fdb.conn=None
			firebird_fdb.cursor=None
			firebird_fdb.state=False
			firebird_fdb.log='Disconnected'
		else:
			firebird_fdb.log='Already disconnected'
		#end of function suite
		return

	#end of class suite


#Вывод ошибок возвращается "в исходное положение"

#sys.stderr.close()
#sys.stderr = sys.__stderr__